<?php
require_once ("../src/db/connectdatabase.php");

function getPersonalRate($id) {
	$con = new ConnectDB();
	$tbl = "personalrate";
	$fn = array('rate_id', 'bankname', 'cata', 'catb', 'others', 'prepaymentcharge', 'processfee', 'dateupdate', 'bankid');
	$condetion = array('rate_id' => $id);
	$flike = array();
	return $con -> getvalueall($tbl, $fn, $condetion, $flike, '');
}


if(isset($_GET['id'])) {
	$id = $_GET['id'];
	$arrperrate = getPersonalRate($id);
	if(count($arrperrate) == 1) {
?>

<style>
textarea {
	margin:5px;
	padding:5px;
}
</style>
<h3>Cập Nhật Lãi Suất Cho Vay Cá Nhân Ngân Hàng "<?php echo($arrperrate[0][1]) ?>"</h3>
<div style="color: red;">
<?php
	if(isset($_GET['mess'])) {
		echo($_GET['mess']);
	}
?>
</div>
<div>
<form action="../src/control/adminpersonalrate.php" method="post" onSubmit="return checkinfo();">
	<input type="hidden" value="<?php echo($arrperrate[0][0]) ?>" name="idedit" />
	<table border="1" cellpadding="0" cellspacing="0">
    	<tr>
        	<th width="150px" align="left" valign="top">Ngân Hàng</th>
            <td><select style="width: 400px; height: 30px; margin:5px; padding:5px;" class="bankloadselect" name="bankloadselect"><option value="">----Chọn Ngân Hàng----</option></select></td>
        </tr>
        <tr>
        	<th width="150px" align="left" valign="top">CAT A</th>
            <td><textarea cols="70" rows="5" name="cata" id="cata" onFocus="showStyle(this)" onBlur="hiddenStyle(this)"><?php echo($arrperrate[0][2]) ?></textarea></td>
        </tr>
        <tr>
        	<th width="150px" align="left" valign="top">CAT B</th>
            <td><textarea cols="70" rows="5" name="catb" id="catb" onFocus="showStyle(this)" onBlur="hiddenStyle(this)"><?php echo($arrperrate[0][3]) ?></textarea></td>
        </tr>
        <tr>
        	<th width="150px" align="left" valign="top">Khác</th>
            <td><textarea cols="70" rows="5" name="other" id="other" onFocus="showStyle(this)" onBlur="hiddenStyle(this)"><?php echo($arrperrate[0][4]) ?></textarea></td>
        </tr>
        <tr>
        	<th width="150px" align="left" valign="top">Phí Trả Trước</th>
            <td><input type="text" class="inputtextfield" name="phitratruoc" id="phitratruoc" onFocus="showStyle(this)" onBlur="hiddenStyle(this)" value="<?php echo($arrperrate[0][5]) ?>" /></td>
        </tr>
        <tr>
        	<th width="150px" align="left" valign="top">Lệ Phí Xử Lý</th>
            <td><input type="text" class="inputtextfield" name="lephixuly" id="lephixuly" onFocus="showStyle(this)" onBlur="hiddenStyle(this)" value="<?php echo($arrperrate[0][6]) ?>" /></td>
        </tr>
        <tr>
        	<th colspan="2"><input type="hidden" name="acc" value="edit" /><input type="submit" value="Lưu" style="border-radius: 5px; padding:5px; width:50px; background:#03F; color:#FFF; font-weight:bold;" /></th>
        </tr>
    </table>
</form>
</div>
<script>
$('#cata').val($('#cata').val().replace(/<br>?/g, "\n"));
$('#catb').val($('#catb').val().replace(/<br>?/g, "\n"));
$('#other').val($('#other').val().replace(/<br>?/g, "\n"));

loadbank();
function loadbank() {
	$.ajax({
		url: "../src/ajax/ajaxbank.php",
		type: "post",
		data: {act:'getbanks'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + ' (' + entry[0] + '</option>';
			});
			$('.bankloadselect').append(html);
			$(".bankloadselect option[value='<?php echo($arrperrate[0][8]) ?>']").attr('selected', 'selected');
		}
	});
}

function checkinfo() {
	var tennganhang = $.trim($('.bankloadselect').val());
	if(tennganhang.length == 0) {
		alert("Vui lòng chọn thông tin ngân hàng");
		$('.bankloadselect').focus();
		return false;
	}
	
	var cata = $.trim($('#cata').val());
	if(cata.length == 0) {
		alert("Vui lòng nhập thông tin CAT A");
		$('#cata').focus();
		return false;
	} else {
		$('#cata').val(cata.replace(/\n\r?/g, '<br>'));
	}
	
	var catb = $.trim($('#catb').val());
	if(catb.length == 0) {
		alert("Vui lòng nhập thông tin CAT B");
		$('#catb').focus();
		return false;
	} else {
		$('#catb').val(catb.replace(/\n\r?/g, '<br>'));
	}
	
	var other = $.trim($('#other').val());
	if(other.length == 0) {
		alert("Vui lòng nhập thông tin Khác");
		$('#other').focus();
		return false;
	} else {
		$('#other').val(other.replace(/\n\r?/g, '<br>'));
	}
	
	var phitratruoc = $.trim($('#phitratruoc').val());
	if(phitratruoc.length == 0) {
		alert("Vui lòng nhập thông tin phí trả trước");
		$('#phitratruoc').focus();
		return false;
	}
	
	var lephixuly = $.trim($('#lephixuly').val());
	if(lephixuly.length == 0) {
		alert("Vui lòng nhập thông tin lệ phí xử lý");
		$('#lephixuly').focus();
		return false;
	}
}

function showStyle(field) {
	field.style.backgroundColor = '#FFFFCC';
}

function hiddenStyle(field) {
	field.style.backgroundColor = '#FFFFFF';
}
</script>
<?php
	}
}
?>